import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, Form, Input, Popconfirm, Row, Table} from 'antd';
import Constants from '../../utils/Constants';
import RightConstants from '../../utils/RightConstants';
import RightBox from '../../components/common/rightBox';
import EditLocalFeeDetail from '../../components/fee/localFee/editLocalFeeDetail';
import LocalFeeDetail from '../../components/fee/localFee/localFeeDetail';
import ImportFile from '../../components/common/importFile';

const FormItem = Form.Item;

const localFee = Form.create()(class LocalFee extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        this.props.dispatch({ type: 'localFee/getListForPage', payload: { page: Constants.DEFAULT_PAGE } });
    }

    handleOpen = (id, disabled) => {
        this.props.dispatch({
            type: 'localFee/getLocalFeeById',
            payload: { id }
        });
        this.props.dispatch({ type: 'supplier/getSupplierList', payload: { supplierType: Constants.SUPPLIER_TYPE.FRAE } });  //查询物流供应商
        this.props.dispatch({ type: 'airline/getAllAirLines' });   //查询航线
        this.props.dispatch({ type: 'berthingCompany/getAllBerthingCompanys' }); //查询船公司

        this.editLocalFeeDetail.handleOpen(disabled);
    }

    handleImport = (module) =>{
        this.importFile.handleOpen(module);
    }

    handleCopy = (localFeeId) =>{
        this.props.dispatch({
            type: 'localFee/copyLocalFee',
            payload: { 
                localFeeId,
                onSuccess: this.handleSearch
            }
        })
    }

    handleLocalOpen = (id) => {
        this.props.dispatch({
            type: 'localFeeDetail/getListForPage',
            payload: {
                localFeeId: id,
                page: Constants.DEFAULT_PAGE
            }
        });

        this.localFeeDetail.handleOpen(id);
    }

    handleDelete = id => {
        this.props.dispatch({
            type: 'localFee/deleteLocalFeeById',
            payload: {
                id,
                onSuccess: this.handleSearch
            }
        })
    }

    handleSearch = flag => {
        let newPage = this.props.localFee.page;
        if (flag) newPage.page = 1;
        this.props.dispatch({
            type: 'localFee/getListForPage',
            payload: {
                ...this.props.form.getFieldsValue(),
                page: newPage
            }
        });
    };

    columns = [
        { title: '供应商名称/代码', key: 'companyname', width: '220px',
            render: record =>{
                return <span>{record.companyname}({record.supplierCode})</span>
            }    
        },
        { title: '航线名称', dataIndex: 'airlineName', key: 'airlineName  ' },
        { title: '航线代码', dataIndex: 'airlineCode', key: 'airlineCode' },
        { title: '船公司名称', dataIndex: 'berthingName', key: 'berthingName' },
        { title: '船公司代码', dataIndex: 'berthingCode', key: 'berthingCode' },
        { title: '有效开始时间', dataIndex: 'begindate', key: 'begindate' },
        { title: '有效结束时间', dataIndex: 'enddate', key: 'enddate' },
        { title: '附加费',
            render: (record) => {
                let localFeeDetailLink = <a href="#" onClick={this.handleLocalOpen.bind(this, record.id)}>本地费用</a>
                return <span>{localFeeDetailLink}</span>
            }
        },
        {
            title: '操作',
            render: (record) => {
                let editLink = <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_LOCALFEE_EDIT} split={true}><a href="#" onClick={this.handleOpen.bind(this, record.id, false)}>编辑</a></RightBox>
                let deleteLink = <Popconfirm onConfirm={this.handleDelete.bind(this, record.id)} okText="是" cancelText="否"
                    placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除?'}>
                    <a href="#">删除</a>
                </Popconfirm>
                deleteLink = <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_LOCALFEE_DELETE} split={true}>{deleteLink}</RightBox>
                let viewLink = <a href="#" onClick={this.handleOpen.bind(this, record.id, true)}>查看</a>

                let copyLink = <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_LOCALFEE_COPY} split={true}><a href="#" onClick={this.handleCopy.bind(this, record.id)}>复制本地费</a></RightBox>
                return <span>{editLink}{deleteLink}{copyLink}{viewLink}</span>
            }
        }
    ];

    render() {
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };
        const { getFieldDecorator } = this.props.form;
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            pageSize: this.props.localFee.page.size,
            current: this.props.localFee.page.page,
            total: this.props.localFee.page.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = this.props.localFee.page;
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'localFee/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = this.props.localFee.page;
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'localFee/getListForPage',
                    payload: {
                        ...this.props.form.getFieldsValue(),
                        page: newPage
                    }
                });
            }
        }

        return (
            <div>
                <Row>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'供应商'}>
                            {getFieldDecorator('supplier')
                                (
                                <Input maxLength="20" placeholder="可根据供应商代码、中英文名称搜索" />
                                )
                            }
                        </FormItem>
                    </Col>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'航线'}>
                            {getFieldDecorator('airline')
                                (
                                <Input maxLength="200" placeholder="可根据航线代码、名称搜索" />
                                )
                            }
                        </FormItem>
                    </Col>
                </Row>
                <Row>
                    <Col span={6}>
                        <FormItem {...formItemLayout} label={'船公司'}>
                            {getFieldDecorator('berthing')
                                (
                                <Input maxLength="200" placeholder="可根据船公司代码、中英文名称搜索" />
                                )
                            }
                        </FormItem>
                    </Col>
                    <Col span={11} style={{ marginLeft: 42}}>
                        <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this, true)} size={'default'}>查询</Button>
                        <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_LOCALFEE_ADD} >
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleOpen.bind(this, null, false)} size={'default'}>新增本地费用</Button>
                        </RightBox>
                        <RightBox resource={RightConstants.COSTMAINTENANCE.COSTMAINTENANCE_LOCALFEE_IMPORT}>
                            <Button type="primary" icon="plus" style={{ marginRight: 5 }} onClick={this.handleImport.bind(this, Constants.IMPORT_MODULE[4])} size={'default'}>导入本地费</Button>
                        </RightBox>
                    </Col>
                </Row>

                <Table loading={this.props.loading} rowKey='id' dataSource={this.props.localFee.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                <EditLocalFeeDetail wrappedComponentRef={(inst) => this.editLocalFeeDetail = inst} refreshList={this.handleSearch} />
                <LocalFeeDetail wrappedComponentRef={(inst) => this.localFeeDetail = inst} refreshList={this.handleSearch} />
                <ImportFile wrappedComponentRef={(inst) => this.importFile = inst} refreshList={this.handleSearch} />
            </div>
        );
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.localFee, localFee: state.localFee
    };
}

export default connect(mapStateToProps)(localFee);
